package com.example.hybridmobiledemo.module.main.model.news.content;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hybridmobiledemo.R;
import com.example.hybridmobiledemo.databinding.ActivityNewsConBinding;
import com.example.hybridmobiledemo.module.main.bean.ComsAllInfo;
import com.example.hybridmobiledemo.module.main.bean.ComsInfo;
import com.example.hybridmobiledemo.util.OkHttpUtils;
import com.example.hybridmobiledemo.view.ComRvAdapter;
import com.example.hybridmobiledemo.view.NewsRvAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class NewsConActivity extends AppCompatActivity {
    private ActivityNewsConBinding binding;
    private WebView webView;
    private TextView tvConTitle, tvConTime, tvConAuthor;
    private RecyclerView rvConCom;
    private String mUrl;
    private String title;
    private String time;
    private String author;
    private int SCNewsId;
    private ArrayList<ComsInfo> comsList = new ArrayList<ComsInfo>();
    private String UserID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        binding = ActivityNewsConBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        SharedPreferences sp = getSharedPreferences("UserID", Context.MODE_PRIVATE);
        UserID = sp.getString("UserID", "ODky");
        initGetIntent();
        initTitle();
        initView();
        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils.builder()
                        .url("interface/SCNewsComment/getAll")
                        .addParam("userId", UserID)
                        .get()
                        .async(new OkHttpUtils.AsyncCallBack() {
                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                ComsAllInfo info = gson.fromJson(json, ComsAllInfo.class);
                                List<ComsAllInfo.DataBean> list = info.getData();
                                for (int i = 0; i < list.size(); i++) {
                                    int newsId = list.get(i).getsCNewsId();
                                    if (newsId == SCNewsId) {
                                        String name = list.get(i).getAuthor();
                                        String con = list.get(i).getContent();
                                        String unixTime = String.valueOf(list.get(i).getCreateTime());
                                        String time = NewsRvAdapter.TimeStamp2Date(unixTime, "yyyy-MM-dd HH:mm");
                                        comsList.add(new ComsInfo(con, time, name));
                                    }
                                }
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        rvConCom.setLayoutManager(new LinearLayoutManager(NewsConActivity.this));
                                        rvConCom.setAdapter(new ComRvAdapter(comsList));
                                    }
                                });
                            }

                            @Override
                            public void onFailure(Exception e) {

                            }
                        });
            }
        }).start();
    }

    private void initGetIntent() {
        Intent intent = getIntent();
        mUrl = intent.getStringExtra("conUrl");
        title = intent.getStringExtra("title");
        author = intent.getStringExtra("author");
        time = intent.getStringExtra("time");
        SCNewsId = intent.getIntExtra("SCNewsId", 1);
    }

    public static void initWebView(String mUrl, WebView webView) {
        // 设置 WebView 允许执行 JavaScript 脚本
        webView.getSettings().setJavaScriptEnabled(true);

        // 确保跳转到另一个网页时仍然在当前 WebView 中显示
        // 而不是调用浏览器打开
        webView.setWebViewClient(new WebViewClient());

        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);

        // 加载指定网页
        //webView.loadUrl(url);
        webView.loadDataWithBaseURL(null,getHtmlData(mUrl),"text/html","UTF-8",null);
    }

    private void initTitle() {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.new_con_bar);
        TextView tvNewConBack = findViewById(R.id.tvNewConBack);
        ImageView imgNewConBack = findViewById(R.id.imgNewConBack);
        tvNewConBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        imgNewConBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private static String getHtmlData(String bodyHTML) {
        String head = "<head><style>img{max-width: 100%; width:auto; height: auto;}</style></head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    private void initView() {
        webView = binding.webNewsCon;
        tvConTitle = binding.tvConTitle;
        tvConTime = binding.tvConTime;
        tvConAuthor = binding.tvConAuthor;
        rvConCom = binding.rvConCom;
        tvConTitle.setText(title);
        tvConTime.setText(time);
        initWebView(mUrl, webView);
        tvConAuthor.setText("作者：" + author);
    }
}